正则表达式 (RegExp)是一种专门的正式语法,用于描述、匹配和操作字符串数据中的模式。在 JavaScript 中,正则表达式充当引擎执行搜索与验证操作的蓝图。
1. 定义语法
定义模式主要有两种方式: 字面量表示法 (var re2 = /abc/;),在脚本加载时编译,以及 正则表达式构造函数 (var re1 = new RegExp("abc");),可从变量动态构建模式。
2. .test() 方法
该 .test() 方法是应用此语法最基础的方式;它返回一个布尔值,指示模式是否存在于目标字符串的任意位置。某些字符如 + 具有特殊含义,必须通过反斜杠进行 转义 以匹配其字面意义(例如, /eighteen\+/) 才能作为字面字符匹配。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which notation is compiled when the script is first loaded?
The RegExp constructor notation.
The literal value notation (e.g., /abc/).
The string.match() method.
The JSON.parse() notation.
✅ Correct!
Literal notation is static and compiled once when the code is evaluated.❌ Incorrect
The RegExp constructor is evaluated at runtime, making it slower but more flexible for dynamic strings.QUESTION 2
What is the return type of the RegExp .test() method?
String
Number
Boolean
Object
✅ Correct!
Correct! It returns true if the pattern is found and false otherwise.❌ Incorrect
Think about it: does the pattern exist or not? That's a binary choice.QUESTION 3
How do you match a literal plus sign (+) in a regular expression?
By using double quotes.
By escaping it with a backslash: \+.
By placing it in parentheses.
Plus signs are always matched literally by default.
✅ Correct!
Because '+' is a special quantifier (one or more), it must be escaped to be treated as text.❌ Incorrect
Special characters in RegExp grammar require a backslash to be interpreted literally.QUESTION 4
Which flag is used to perform a case-insensitive search?
The 'g' flag.
The 's' flag.
The 'i' flag.
The 'c' flag.
✅ Correct!
The 'i' flag stands for 'insensitive'.❌ Incorrect
'g' is for global matching. Case-insensitivity uses 'i'.QUESTION 5
When would you prefer the RegExp constructor over literal notation?
When the pattern is constant throughout the program.
When you need to build a pattern from a variable string.
When you want better performance.
Literal notation is always preferred.
✅ Correct!
The constructor accepts strings, allowing you to create patterns dynamically based on user input or other variables.❌ Incorrect
Literals are fixed. If your pattern depends on a variable, you must use the constructor.Case Study: Product Code Validation
Applying RegExp to Database Filtering
A logistics company uses product codes that start with the prefix 'abc'. However, legacy systems sometimes enter these in uppercase ('ABC') or mixed case ('aBc'). You need to create a validator that identifies these codes regardless of case.
Q
1. Write the literal regular expression to match 'abc' case-insensitively.
Solution:
The pattern is
The pattern is
/abc/i. The 'i' flag ensures that 'ABC', 'abc', and 'AbC' all return true when tested.Q
2. If you were receiving the prefix as a user-provided string variable named 'prefix', how would you initialize the RegExp?
Solution:
You would use the constructor:
You would use the constructor:
new RegExp(prefix, "i"). This allows the pattern to be dynamic while still applying the case-insensitive flag.Q
3. Why does
/abc/.test("abcde") return true even though the strings aren't identical?Solution:
By default, a regular expression looks for the pattern anywhere within the string, not just an exact full-string match. Since 'abc' exists at the start of 'abcde', the test passes.
By default, a regular expression looks for the pattern anywhere within the string, not just an exact full-string match. Since 'abc' exists at the start of 'abcde', the test passes.